/**
 * Created by chenhong on 17/2/20.
 */

//弹窗
function openDialog(title,url,w,h,isFull){
    layer_show(title,url,w,h,isFull);
}

function request(url,callback,params,method,yes) {
    var yescallback = yes || function () { layer.close(errorindexs)};
    $.ajax({
        url:url,
        data:params,
        method:method ? method : 'get',
        success:function (data) {
            if (data.code != undefined){
                var result = parse(data,yescallback);
                if (result){
                    callback(data.data,data.code,data.msg);
                }
            } else {
                callback(data);
            }
        },
        error:function (response) {
            if (response.status == 422){
                var errorObj = JSON.parse(response.responseText);
                for (var name in errorObj){
                    errorindexs = layer.confirm(errorObj[name][0], {
                        btn: ['确定','取消'], //按钮
                    },yescallback);
                }
            }
            if (response.status == 500){
                errorindexs = layer.confirm('数据请求失败,请关闭页面重试!', {
                    btn: ['确定','取消'], //按钮
                },yescallback);
            }
        }
    });
}

    function parse(data,yescallback) {
    if (data.code != 0){
        errorindexs = layer.confirm(data.msg, {
            btn: ['确定','取消'] //按钮
        },yescallback);
        return false;
    }
    return data;
}
